#include "StdAfx.h"
#include "BasicWindowLessTheme.h"

namespace WINDOWLESSUI
{
	Image* ThemeLoadResID(HINSTANCE hInstance,UINT uID,HGLOBAL& hAlloc);

	CBasicWindowLessTheme gMyTheme;

	CBasicWindowLessTheme::CBasicWindowLessTheme(void)
	{
	}

	CBasicWindowLessTheme::~CBasicWindowLessTheme(void)
	{
	}

	void CBasicWindowLessTheme::InitTheme(HINSTANCE hInstance)
	{

		UINT uID[] = {	//IDB_PNG_BTN,
			//IDB_PNG_BUTTONCATE              ,
			//IDB_PNG_GOU                     ,
			//IDB_PNG_IMG_BG                  ,
			////IDB_PNG_SHOW                    ,
			////IDB_PNG_TAB                     ,
			//IDB_PNG_SCROLLBAR_BKGND			,
			//IDB_PNG_SCROLLBAR_THUMB			,
			//IDB_PNG_PROGRESS				,
			//IDB_PNG_PROGRESS_BKG			,
			IDB_PNG_COMMONBTN               ,
			//IDB_PNG_SLIDEBAR2               ,
			//IDB_PNG_SLIDEBAR                ,
			//IDB_PNG_SLIDETHUMB              ,
			//IDB_PNG_SMTIP					,
			//IDB_PNG_TIPS					,
		};

		for(int i=0; i<sizeof(uID)/sizeof(uID[0]);i++)
		{
			HGLOBAL hAlloc;
			Image*  pIm = ThemeLoadResID(hInstance,uID[i],hAlloc);
			if (pIm)
			{
				gMyTheme.m_resMap.insert(map<int,Image*>::value_type(uID[i],pIm));
				gMyTheme.m_hGlobalMap.insert(map<Image*,HGLOBAL>::value_type(pIm,hAlloc));
			}
		}

	}


	Image* CBasicWindowLessTheme::GetImage(UINT uID)
	{
		map<int,Image*>::iterator it = gMyTheme.m_resMap.find(uID);
		if (it != gMyTheme.m_resMap.end())
			return it->second;
		return NULL;
	}

	extern HINSTANCE gDllhInstance;
	wstring CBasicWindowLessTheme::GetResString(UINT uID)
	{
		wstring str;
		wchar_t buf[512];
		buf[0] = 0;
		::LoadString(gDllhInstance,uID,buf,512);
		str = buf;
		return str;
	}

	void CBasicWindowLessTheme::UnitTheme()
	{
		map<int,Image*>::iterator it = gMyTheme.m_resMap.begin();

		while(it != gMyTheme.m_resMap.end())
		{
			map<Image*,HGLOBAL>::iterator itH = gMyTheme.m_hGlobalMap.find(it->second);
			delete it->second;
			if (itH !=  gMyTheme.m_hGlobalMap.end())
			{
				::GlobalUnlock(itH->second);
				::GlobalFree(itH->second);
			}
			it++;
		}

		gMyTheme.m_hGlobalMap.clear();
		gMyTheme.m_resMap.clear();
	}

	Image* ThemeLoadResID(HINSTANCE hInstance,UINT uID,HGLOBAL& hAlloc)
	{
		HRSRC hResource;
		DWORD imageSize;
		Image* pIm = NULL;
		bool bOk = false;

		hResource = ::FindResource(hInstance, MAKEINTRESOURCE(uID),L"PNG");

		if (hResource == NULL)
			return NULL;

		if ((imageSize = ::SizeofResource(hInstance, hResource)) == 0)
			return NULL;

		const void* pResourceData = ::LockResource(::LoadResource(hInstance, hResource));

		if (!pResourceData)
		{
			return NULL;
		}

		HGLOBAL g_Buffer  = ::GlobalAlloc(GMEM_FIXED, imageSize);

		if (g_Buffer)
		{
			void* pBuffer = ::GlobalLock(g_Buffer);
			if (pBuffer)
			{
				CopyMemory(pBuffer, pResourceData, imageSize);

				IStream* pStream = NULL;
				if (::CreateStreamOnHGlobal(g_Buffer, FALSE, &pStream) == S_OK)
				{
					pIm = Image::FromStream(pStream);
					pStream->Release();
					hAlloc = g_Buffer;
					return pIm;
				}
				::GlobalUnlock(g_Buffer);
			}
			::GlobalFree(g_Buffer);
		}
		return NULL;
	}
}
